const db = require("../db/index");
//----------------------------------------------------------------
// 获取所有的列表
exports.labList = (req, res) => {
  var pagenum = req.query.pagenum; //页码值
  var pagesize = req.query.pagesize; //每页显示数据
  var lab_class_name = req.query.lab_class_name;
  var lab_address = req.query.lab_address;
  var lab_status = req.query.lab_status;
  // console.log(lab_class_name);
  // console.log(lab_address);
  // console.log(lab_status);
  const count_value = (pagenum - 1) * pagesize; //查询的起始值
  var sql = null;
  if (lab_class_name) {
    sql = `select * from lab_class where is_delete = 0 and lab_class_name like"${lab_class_name}%" `;
  }
  if (lab_address) {
    sql = `select * from lab_class where is_delete = 0 and lab_address like"%${lab_address}%" `;
  }
  if (lab_status == 1) {
    sql = `select * from lab_class where is_delete = 0 and residue = 0 `;
  }
  if (lab_status == 2) {
    sql = `select * from lab_class where is_delete = 0 and residue > 0 `;
  }
  if (lab_class_name && lab_status == 1) {
    sql = `select * from lab_class where is_delete = 0 and lab_class_name like"${lab_class_name}%" and residue = 0 `;
  }
  if (lab_class_name && lab_status == 2) {
    sql = `select * from lab_class where is_delete = 0 and lab_class_name like"${lab_class_name}%" and residue > 0 `;
  }
  if (lab_class_name && lab_address) {
    sql = `select * from lab_class where is_delete = 0 and lab_class_name like"${lab_class_name}%" and lab_address like"%${lab_address}%" `;
  }
  if (lab_status == 1 && lab_address) {
    sql = `select * from lab_class where is_delete = 0 and lab_address like"%${lab_address}%" and residue = 0 `;
  }
  if (lab_status == 2 && lab_address) {
    sql = `select * from lab_class where is_delete = 0 and lab_address like"%${lab_address}%" and residue > 0  `;
  }
  if (lab_class_name && lab_address && lab_status === 1) {
    sql = `select * from lab_class where is_delete = 0 and lab_class_name like"${lab_class_name}%" and lab_address like"%${lab_address}%" and residue = 0 `;
  }
  if (lab_class_name && lab_address && lab_status === 2) {
    sql = `select * from lab_class where is_delete = 0 and lab_class_name like"${lab_class_name}%" and lab_address like"%${lab_address}%" and residue > 0`;
  }
  if (!lab_class_name && !lab_address && !lab_status) {
    sql = `select * from lab_class where is_delete = 0 `;
  }
  const countStr = `select count(*) as total from(${sql}) as labClass `;
  db.query(countStr, (err, results) => {
    // console.log(results);
    if (err) return res.cc(err);
    const sum = results[0].total;
    // console.log(sum);
    sql += `limit ${count_value}, ${pagesize}`;
    db.query(sql, (err, results) => {
      if (err) return res.cc(err);
      if (results.length == 0) return res.cc("没有相关信息！");
      // console.log(results);
      res.send({
        status: 0,
        message: "获取信息成功！",
        data: results,
        total: sum,
      });
    });
  });
};

exports.getListAdmin = (req, res) => {
  var pagenum = req.query.pagenum; //页码值
  var pagesize = req.query.pagesize; //每页显示数据
  var lab_class_name = req.query.lab_class_name; //宿舍管理员
  var lab_address = req.query.lab_address; //楼栋
  const count_value = (pagenum - 1) * pagesize; //查询的起始值
  var sql = null;
  if (lab_class_name) {
    sql = `select * from lab_device_cate where is_delete = 0 and admin_user like"${lab_class_name}%" `;
  }
  if (lab_address) {
    sql = `select * from lab_device_cate where is_delete = 0 and cate_name like"%${lab_address}%" `;
  }
  if (lab_class_name && lab_address) {
    sql = `select * from lab_device_cate where is_delete = 0 and admin_user like"${lab_class_name}%" and cate_name like"%${lab_address}%" `;
  }
  if (!lab_class_name && !lab_address) {
    sql = `select * from lab_device_cate where is_delete = 0 `;
  }
  const countStr = `select count(*) as total from(${sql}) as labClass `;
  db.query(countStr, (err, results) => {
    // console.log(results);
    if (err) return res.cc(err);
    const sum = results[0].total;
    // console.log(sum);
    sql += `limit ${count_value}, ${pagesize}`;
    db.query(sql, (err, results) => {
      if (err) return res.cc(err);
      if (results.length == 0) return res.cc("没有相关信息！");
      // console.log(results);
      res.send({
        status: 0,
        message: "获取信息成功！",
        data: results,
        total: sum,
      });
    });
  });
};
//----------------------------------------------------------------
// 新增
exports.addLab = (req, res) => {
  // const lab_class_add_time = new Date();
  const lab_class_name = req.body.lab_class_name;
  const lab_address = req.body.lab_address;
  const gross = req.body.gross;
  var lou_type = null;
  var data = {
    lab_class_name: lab_class_name,
    lab_address: lab_address,
    gross: gross,
    residue: gross,
    lou_type: lou_type,
  };
  // console.log(lab_address);
  const sql = `select * from lab_class where lab_address = ? and lab_class_name = ? and is_delete = 0 `;
  db.query(sql, [lab_address, lab_class_name], (err, results) => {
    if (err) return res.cc(err);
    if (results.length > 0) return res.cc("此楼栋已有此宿舍，请更换重试！");
    // console.log(results);
    const sqll = `select lou_type from lab_device_cate where cate_name = ? and is_delete = 0 `;
    db.query(sqll, lab_address, (err, results) => {
      if (err) return res.cc(err);
      if (results.length == 0) return res.cc("请更换重试！");
      // console.log(results);
      data.lou_type = results[0].lou_type;
      // console.log(lou_type);
      const sql = `insert into lab_class set ? `;
      db.query(sql, data, (err, results) => {
        if (err) return res.cc(err);
        if (results.affectedRows !== 1) return res.cc("新增失败！");
        res.cc("新增成功！", 0);
      });
    });
  });
};
//----------------------------------------------------------------
// 根据id获取信息
exports.getLab = (req, res) => {
  const id = req.params.id;
  // console.log(id);
  const sql = `select * from lab_class where id = ? and is_delete = 0 `;
  db.query(sql, id, (err, results) => {
    // console.log(results);
    if (err) return res.cc(err);
    if (results.length === 0) return res.cc("获取本信息失败！");

    res.send({
      status: 0,
      message: "获取信息成功！",
      data: results,
    });
  });
};
//--------------------------------
// 根据id修改信息
exports.editLab = (req, res) => {
  const id = req.body.id;
  const lab_class_name = req.body.lab_class_name;
  const lab_address = req.body.lab_address;
  var lou_type = null;
  var data = {
    lab_class_name: lab_class_name,
    lab_address: lab_address,
    lou_type: lou_type,
  };
  const sql = `select * from lab_class where id <> ? and lab_class_name = ? and is_delete = 0 `;
  db.query(sql, [id, lab_class_name], (err, results) => {
    if (err) return res.cc(err);
    if (results.length === 1)
      return res.send({
        status: 1,
        message: "宿舍名称重复，请更换后重试！",
      });
    if (results.length === 1 && results[0].lab_class_name === lab_class_name)
      return res.cc("宿舍名称重复，请更换后重试！", 1);
    const sqll = `select lou_type from lab_device_cate where cate_name = ? and is_delete = 0 `;
    db.query(sqll, lab_address, (err, results) => {
      if (err) return res.cc(err);
      if (results.length == 0) return res.cc("请更换重试！");
      // console.log(results);
      data.lou_type = results[0].lou_type;
      // console.log(lou_type);
      const sql = `update lab_class set ? where id = ? `;
      db.query(sql, [data, id], (err, results) => {
        if (err) return res.cc(err);
        if (results.affectedRows !== 1) return res.cc("更新名称失败！");
        res.cc("更新名称成功！", 0);
      });
    });
  });
};
// ----------------------------------------------------------------
// 根据id删除
exports.deleteLab = (req, res) => {
  const id = req.params.id;
  // console.log(id);
  const sql = `update lab_class set is_delete = 1 where id = ? `;
  db.query(sql, id, (err, result) => {
    if (err) return res.cc(err);
    if (result.affectedRows !== 1) return res.cc("删除失败！");
    res.cc("删除成功！", 0);
  });
};
// ----------------------------------------------------------------
// 预约
exports.labAppointment = (req, res) => {
  const id = req.params.id;
  const name = req.user.username;
  // console.log(req);
  // console.log(name);
  // console.log(id);
  const sql = `select * from lab_class where lab_class_appointment_person = ? and id = ? and lab_status = 1 `;
  db.query(sql, [name, id], (err, results) => {
    // console.log(results);
    if (err) return res.cc(err);
    if (results.length == 1) return res.cc("此已被预约，请选择其他", 1);
    const sql = `update lab_class set lab_class_appointment_person = ? , lab_status = 1 where id = ? `;
    db.query(sql, [name, id], (err, result) => {
      if (err) return res.cc(err);
      if (result.affectedRows !== 1) return res.cc("预约失败！");
      res.cc("预约成功！", 0);
      const sql = `select * from lab_class where id = ? `;
      db.query(sql, id, (err, results) => {
        if (err) return res.cc(err);
        if (results.length == 0) return res.cc("未查询到此", 1);
        const lab_class_name = results[0].lab_class_name;
        const lab_class_person = results[0].lab_class_person;
        const start_time = new Date();
        var data = {
          lab_class_name: lab_class_name,
          lab_class_person: lab_class_person,
          start_time: start_time,
          lab_class_appointment_person: name,
        };
        const sql = `insert into lab_log set ?`;
        db.query(sql, data, (err, result) => {
          if (err) return res.cc(err);
          if (result.affectedRows !== 1) return res.cc("添加预约记录失败");
          // res.cc('添加预约记录成功', 0)
        });
      });
    });
  });
};
// ----------------------------------------------------------------
// 归还预约
exports.labCancel = (req, res) => {
  const id = req.params.id;
  const name = req.user.username;
  // console.log(req);
  // console.log(name);
  // console.log(id);
  const sql = `select * from lab_class where id = ? and lab_status = 1 `;
  db.query(sql, id, (err, results) => {
    // console.log(results[0]);
    if (err) return res.cc(err);
    if (results[0].lab_class_appointment_person != name)
      return res.cc("此不是您预约的，只能由预约人归还", 1);
    const lab_class_name = results[0].lab_class_name;
    // console.log(lab_class_name);
    const sql = `update lab_class set lab_class_appointment_person = null, lab_status = 0 where id = ? `;
    db.query(sql, id, (err, result) => {
      if (err) return res.cc(err);
      if (result.affectedRows !== 1) return res.cc("归还失败！", 1);
      const end_time = new Date();
      // console.log(end_time);
      const sql = `update lab_log set end_time=? where end_time is null and lab_class_name='${lab_class_name}' `;
      db.query(sql, end_time, (err, result) => {
        if (err) return res.cc(err);
        if (result.affectedRows !== 1) return res.cc("添加结束预约记录失败");
        res.cc("归还成功！", 0);
      });
    });
  });
};
//----------------------------------------------------------------
// 获取预约记录列表信息
exports.lab_log_List = (req, res) => {
  var pagenum = req.query.pagenum; //页码值
  var pagesize = req.query.pagesize; //每页显示数据
  var lab_class_name = req.query.lab_class_name;
  var lab_class_appointment_person = req.query.lab_class_appointment_person;
  // console.log(lab_status);
  const count_value = (pagenum - 1) * pagesize; //查询的起始值
  var sql = null;
  if (lab_class_name) {
    sql = `select * from lab_log where lab_class_name like"%${lab_class_name}%" `;
  }
  if (lab_class_appointment_person) {
    sql = `select * from lab_log where lab_class_appointment_person like"%${lab_class_appointment_person}%" `;
  }
  if (lab_class_name && lab_class_appointment_person) {
    sql = `select * from lab_log where lab_class_name like"%${lab_class_name}%" and lab_class_appointment_person like"%${lab_class_appointment_person}%" `;
  }
  if (!lab_class_name && !lab_class_appointment_person) {
    sql = `select * from lab_log order by id desc `;
  }
  const countStr = `select count(*) as total from(${sql}) as labLog `;
  db.query(countStr, (err, results) => {
    if (err) return res.cc(err);
    const sum = results[0].total;
    // console.log(sum);
    sql += `limit ${count_value}, ${pagesize}`;
    db.query(sql, (err, results) => {
      // console.log(results[0]);
      if (err) return res.cc(err);
      if (results.length === 0) return res.cc("未查询到预约记录");
      res.send({
        status: 0,
        message: "获取预约记录成功！",
        data: results,
        total: sum,
      });
    });
  });
};

// 获取所有的列表
exports.labElectricList = (req, res) => {
  var pagenum = req.query.pagenum; //页码值
  var pagesize = req.query.pagesize; //每页显示数据
  var lab_class_name = req.query.lab_class_name;
  var lab_address = req.query.lab_address;
  var lab_status = req.query.lab_status;
  // console.log(lab_class_name);
  // console.log(lab_address);
  // console.log(lab_status);
  const count_value = (pagenum - 1) * pagesize; //查询的起始值
  var sql = null;
  if (lab_class_name) {
    sql = `select * from lab_class where is_delete = 0 and lab_class_name like"${lab_class_name}%" `;
  }
  if (lab_address) {
    sql = `select * from lab_class where is_delete = 0 and lab_address like"%${lab_address}%" `;
  }
  if (lab_status == 0) {
    sql = `select * from lab_class where is_delete = 0 and residue = 0 `;
  }
  if (lab_status > 0) {
    sql = `select * from lab_class where is_delete = 0 and residue > 0 `;
  }
  if (lab_class_name && lab_status == 0) {
    sql = `select * from lab_class where is_delete = 0 and lab_class_name like"${lab_class_name}%" and residue = 0 `;
  }
  if (lab_class_name && lab_status > 0) {
    sql = `select * from lab_class where is_delete = 0 and lab_class_name like"${lab_class_name}%" and residue > 0 `;
  }
  if (lab_class_name && lab_address) {
    sql = `select * from lab_class where is_delete = 0 and lab_class_name like"${lab_class_name}%" and lab_address like"%${lab_address}%" `;
  }
  if (lab_status == 1 && lab_address) {
    sql = `select * from lab_class where is_delete = 0 and lab_address like"%${lab_address}%" and residue = 0 `;
  }
  if (lab_status == 2 && lab_address) {
    sql = `select * from lab_class where is_delete = 0 and lab_address like"%${lab_address}%" and residue > 0  `;
  }
  if (lab_class_name && lab_address && lab_status === 0) {
    sql = `select * from lab_class where is_delete = 0 and lab_class_name like"${lab_class_name}%" and lab_address like"%${lab_address}%" and residue = 0 `;
  }
  if (lab_class_name && lab_address && lab_status > 0) {
    sql = `select * from lab_class where is_delete = 0 and lab_class_name like"${lab_class_name}%" and lab_address like"%${lab_address}%" and residue > 0`;
  }
  if (!lab_class_name && !lab_address && !lab_status) {
    sql = `select * from lab_class where is_delete = 0 `;
  }
  const countStr = `select count(*) as total from(${sql}) as labClass `;
  db.query(countStr, (err, results) => {
    // console.log(results);
    if (err) return res.cc(err);
    const sum = results[0].total;
    // console.log(sum);
    sql += `limit ${count_value}, ${pagesize}`;
    db.query(sql, (err, results) => {
      if (err) return res.cc(err);
      if (results.length == 0) return res.cc("没有相关信息！");
      // console.log(results);
      res.send({
        status: 0,
        message: "获取信息成功！",
        data: results,
        total: sum,
      });
    });
  });
};

// 根据id修改信息
exports.editLabElectric = (req, res) => {
  //   console.log(res.body);
  const id = req.body.id;
  const electric = req.body.electric;
  var data = {
    electric,
  };
  const sql = `update lab_class set ? where id = ? `;
  db.query(sql, [data, id], (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("更新失败！");
    res.cc("👌", 0);
  });
};
